<?php
namespace WXAPI\Controller;
use Think\Controller;
use OSS\OssClient;
use OSS\Core\OssException;
ini_set('max_execution_time', '0');
ini_set('memory_limit', '888M');
class OrderController extends BaseController{
    /**
     * 析构流函数
     */
    public function  __construct() {
        parent::__construct();
    }
    
    public function index(){
        echo I('callback')."(".json_encode([
            'code'=>'200',
            'infos'=>'连接成功！'
        ]).")";
    }
    /**
     * 同步订单到贝菲锁具
     * POST
     * https://api.dinviet.cn/api/third/tlc?token=31d94fcb27ab899771b1e2808447e6cf&type=order
     */
    public function send_orders($order_id = 0){
        //如果有缓存，证明已经传过数据，那么需要去最新的数据7430913
        //echo tpCache("send_orders");die();
        $where = "store_id = 4031";
        if($order_id == 0){
            if(tpCache("send_orders") != null){
                $where.=' and order_id > '.tpCache("send_orders");
            }else{
                $sync_order_id = M('sync_order')->where(['id'=>1])->getField('sync_order_id');
                if($sync_order_id != null){
                     $where.=' and order_id > '.$sync_order_id;
                }
            }
        }else{
            $where.=' and order_id = '.$order_id;
        }    
        
        $data_count =  M('order')->where($where)->count();
        //判断是否有最新的数据，没有就返回
        if($data_count <= 0){
            return;
        }
        // tpCache("send_orders",7430913);

        $Page = intval($data_count / 100);
        if($data_count % 100 > 0){
            $Page = $Page + 1;
        }
        
        for ($i=0; $i<$Page; $i++){
            $cPage = $i;
            $cPage = $cPage * 100;
            $data =  M('order')->where($where)->limit($cPage, 100)->select();
            if($data){
                $count = 0;
                foreach ($data as $key=>$value){
                    $order_goods =  M('order_goods')->field('rec_id,order_id,goods_id,goods_name,goods_num,market_price,goods_price,cost_price,member_goods_price,give_integral,spec_key,spec_key_name,prom_msg,is_comment,prom_type,prom_id,is_send,delivery_id,sku,store_id,commission,is_checkout,deleted,distribut,distribut_price,is_order_refund,jifen_price,points,floating')->where('order_id='.$value['order_id'])->select();
                    if($order_goods){
                        foreach ($order_goods as $keys=>$values){
                            $order_goods[$keys]['goods_name'] = addslashes($values['goods_name']);//addslashes 转义
                        }
                        $data[$key]['product data'] = $order_goods;
                    }else{
                        $data[$key]['product data'] = [];
                    }
                    //判断是否最后一个数据
                    if($i == $Page - 1){
                        $count++;
                        if ($count == count($data)  && $order_id == 0) {
                            tpCache("send_orders",$value['order_id'],2592000000);
                            M('sync_order')-> where('id=1')->setField('sync_order_id',$value['order_id']);
                        }
                    }
                    unset($order_goods);
                }
                
                $json_data=array("token"=>"9172a524249c4e0641cc12b69ac3c1bf","data"=>json_encode($data,true),"type"=>'order');//post参数
                $header = array('Authorization: put_order');
                $put_order = $this->https_post('https://api.dinviet.cn/api/third/tlc',$json_data);
            }
        }
        
        $put_data = -1;
        if ($order_id > 0) {
            $put_data = json_decode($put_order,true)['code'];
        }
        
        $json_str = json_encode(['status'=>0,'put_data'=>$put_data,'where'=>$where,'data_count'=>$data_count,'data'=>[],'msg'=>'订单同步数据完成'],true);
        exit($json_str);
        
    }


    /**
     * 同步订单到大立商贸
     * POST
     * https://api.hr.jialans.cn/api/third/tlc?token=31d94fcb27ab899771b1e2808447e6cf&type=order
     */
    public function send_4268_orders($order_id = 0){
        //如果有缓存，证明已经传过数据，那么需要去最新的数据7430913
        //echo tpCache("send_orders_4268");die();
        $where = "store_id = 4268";
        if($order_id == 0){
            if(tpCache("send_orders_4268") != null){
                $where.=' and order_id > '.tpCache("send_orders_4268");
            }else{
                $sync_order_id = M('sync_order')->where(['id'=>2])->getField('sync_order_id');
                if($sync_order_id != null && $sync_order_id != 0){
                     $where.=' and order_id > '.$sync_order_id;
                }
            }
        }else{
            $where.=' and order_id = '.$order_id;
        }

        $data_count =  M('order')->where($where)->count();
        //判断是否有最新的数据，没有就返回
        if($data_count <= 0){
            return;
        }
        // tpCache("send_orders_4268",7430913);

        $Page = intval($data_count / 100);
        if($data_count % 100 > 0){
            $Page = $Page + 1;
        }
        
        for ($i=0; $i<$Page; $i++){
            $cPage = $i;
            $cPage = $cPage * 100;
            $data =  M('order')->where($where)->limit($cPage, 100)->select();
            if($data){
                $count = 0;
                foreach ($data as $key=>$value){
                    $order_goods =  M('order_goods')->field('rec_id,order_id,goods_id,goods_name,goods_num,market_price,goods_price,cost_price,member_goods_price,give_integral,spec_key,spec_key_name,prom_msg,is_comment,prom_type,prom_id,is_send,delivery_id,sku,store_id,commission,is_checkout,deleted,distribut,distribut_price,is_order_refund,jifen_price,points,floating')->where('order_id='.$value['order_id'])->select();
                    if($order_goods){
                        foreach ($order_goods as $keys=>$values){
                            $order_goods[$keys]['goods_name'] = addslashes($values['goods_name']);//addslashes 转义
                        }
                        $data[$key]['product data'] = $order_goods;
                    }else{
                        $data[$key]['product data'] = [];
                    }
                    //判断是否最后一个数据
                    if($i == $Page - 1){
                        $count++;
                        if ($count == count($data) && $order_id == 0) {
                            tpCache("send_orders_4268",$value['order_id'],2592000000);
                            M('sync_order')-> where('id=2')->setField('sync_order_id',$value['order_id']);
                        }
                    }
                    unset($order_goods);
                }
                
                $json_data=array("token"=>"9172a524249c4e0641cc12b69ac3c1bf","data"=>json_encode($data,true),"type"=>'order');//post参数
                $header = array('Authorization: put_order');
                $put_order = $this->https_post('https://api.hr.jialans.cn/api/third/tlc',$json_data);
            }
        }
        $put_data = -1;
        if ($order_id > 0) {
            $put_data = json_decode($put_order,true)['code'];
        }
        
        $json_str = json_encode(['status'=>0,'put_data'=>$put_data,'where'=>$where,'data_count'=>$data_count,'data'=>[],'msg'=>'订单同步数据完成'],true);
        exit($json_str);
        
    }

    public function https_post($url,$data){
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        
        $result = curl_exec($curl);
        if(curl_errno($curl)) {
            return 'Errno'.curl_errno($curl);
        }
        curl_close($curl);
        return $result;
    }
    
}
